package com.attilax.visual;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;

import org.apache.commons.io.FileUtils;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;

import com.attilax.collection.mapBuilder;
import com.attilax.io.BreakException;
import com.google.common.base.Joiner;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

import officefile.CellAti;
import officefile.excelUtil;
import officefile.excelUtil2007ver;

@SuppressWarnings("all")
public class chartcloud {

	public static void main(String[] args) throws IOException {
		String tmp = "C:\\0wkspc\\FulltxtLucenePrj\\src\\com\\attilax\\visual\\baiduWordcloudJsontmplt.txt";
		String t = FileUtils.readFileToString(new File(tmp));
		List<Map> li = Lists.newArrayList();
		String filePath = "C:\\Users\\attilax\\Documents\\lyricsWords4.txt700song.xlsx";
		excelUtil2007ver.trave_2007fmt(filePath, "coreword", new Consumer<CellAti>() {

			@Override
			public void accept(CellAti CellAti1) {

				if (CellAti1.rowIndex > 300)
					throw new BreakException();
				Map<Integer, Object> row_map = getLiIdex(CellAti1.rowIndex, li);

				row_map.put(CellAti1.cellIndex, CellAti1.val);
				// String colval = cell.getStringCellValue();

			}
		});

		// List<String> li = t2(t, filePath);

		System.out.println(Joiner.on(",").join(li));
		// String t_word=FileUtils.readFileToString(new File(word));

	}

	protected static Map<Integer, Object> getLiIdex(int rowindex, List<Map> li) {

		if (rowindex >= li.size()) {

			li.add(Maps.newLinkedHashMap());
		}

		while (true) {
			try {
				return li.get(rowindex);
			} catch (IndexOutOfBoundsException e) {
				li.add(Maps.newLinkedHashMap());
			}
		}

		// return li .get(rowindex);
	}

	private static List<String> t2(String t, String filePath) throws FileNotFoundException, IOException {
		List<String> li = Lists.newArrayList();
		XSSFSheet sheet = excelUtil2007ver.getsheet(filePath, "coreword");
		for (int rowNum = 0; rowNum <= sheet.getLastRowNum(); rowNum++) {
			if (rowNum > 250)
				break;
			XSSFRow row_cur = sheet.getRow(rowNum);
			Map<Integer, Object> row_map = new LinkedHashMap<Integer, Object>();
			if (row_cur == null)
				continue;
			for (int cellNum_index = row_cur.getFirstCellNum(); cellNum_index < row_cur
					.getLastCellNum(); cellNum_index++) {
				XSSFCell cell = row_cur.getCell(cellNum_index);

				if (cellNum_index == 0)
					row_map.put(cellNum_index, cell.getStringCellValue());
				if (cellNum_index == 1)
					row_map.put(cellNum_index, cell.getNumericCellValue());

			} // trave row finish

			try {
				String t2 = t.replaceAll("@word@", row_map.get(0).toString()).replaceAll("@val@",
						row_map.get(1).toString());
				li.add(t2);
			} catch (Exception e) {
				// TODO: handle exception
			}

		} // trave tab finish
		return li;
	}

}
